﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;
using System.Windows;

namespace LEDScreenLinkTool.Util
{
    public class ShapeHitCheck
    {
        public static bool CheckIsPointOnLineSegment(Point point, Point APoint, Point BPoint, double epsilon = 0.1)
        {
            // 以下是另一个方法，以下方法性能比上面一个好
            // X1 == X2 和 Y1 == Y2 的计算

            var minX = Math.Min(APoint.X, BPoint.X);
            var maxX = Math.Max(APoint.X, BPoint.X);

            var minY = Math.Min(APoint.Y, BPoint.Y);
            var maxY = Math.Max(APoint.Y, BPoint.Y);

            if (!(minX <= point.X) || !(point.X <= maxX) || !(minY <= point.Y) || !(point.Y <= maxY))
            {
                return false;
            }

            // 以下处理水平和垂直线段
            if (Math.Abs(APoint.X - BPoint.X) < epsilon)
            {
                // 如果 X 坐标是相同，那么只需要判断点的 X 坐标是否相同
                // 因为在上面代码已经判断了 点的 Y 坐标是在线段两个点之内
                return Math.Abs(APoint.X - point.X) < epsilon || Math.Abs(BPoint.X - point.X) < epsilon;
            }

            if (Math.Abs(APoint.Y - BPoint.Y) < epsilon)
            {
                return Math.Abs(APoint.Y - point.Y) < epsilon || Math.Abs(BPoint.Y - point.Y) < epsilon;
            }

            if (Math.Abs((point.X - APoint.X) / (BPoint.X - APoint.X) - (point.Y - APoint.Y) / (BPoint.Y - APoint.Y)) < epsilon)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
